package Response;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.ResultSet;

public class StoreQuestion extends HttpServlet {

	PreparedStatement ps=null;
	PreparedStatement pstmt=null;
	Connection con=null;
	ResultSet rs=null;

	public void init() throws ServletException {

		try 
		{

			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/websurvey";
			con=DriverManager.getConnection(url, "root", "mysql");
			System.out.println("connection established");
			
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try 
		{
			
			PreparedStatement ps=con.prepareStatement("insert into question(question,surveyno,qtype)values(?,?,?)");
				
			String question=request.getParameter("question");
			String ans=request.getParameter("option");
			String qtype=request.getParameter("type");
			//out.println(question);
			//out.println(ans);
			String lines[] = ans.split("\\n");
			for(int i = 0; i < lines.length; i++)
			{
				out.println(lines[i]);
			}
			ps.setString(1,question);
			ps.setInt(2,1);
			ps.setString(3,qtype);
			
			pstmt=con.prepareStatement("select qno from question");
			
			 rs=(ResultSet) pstmt.executeQuery();
			
			int r=ps.executeUpdate();
			out.print(r);
			
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		
		out.flush();
		out.close();
	}

	
	public void destroy() {
		super.destroy(); 
	}


}
